<template>
  <div class="container">
    <el-card>
      <el-form ref="subRef" :model="subFrom" :inline="true">
        <el-form-item style="padding-left: 5px" label="学科名称">
          <el-input size="small " v-model="subFrom.user"></el-input>
        </el-form-item>

        <el-form-item>
          <el-row type="flex" justify="space-between" style="width: 100%">
            <el-col>
              <el-button size="small " @click="clearIpt">清除</el-button>
              <el-button size="small " type="primary" @click="onSubmit"
                >搜索</el-button
              >
            </el-col>
            <el-col :span="1">
              <el-button
                class="addbtn"
                type="success"
                @click="showDialog = true"
                ><i class="el-icon-edit"></i>新增学科</el-button
              >
            </el-col>
          </el-row>
        </el-form-item>

        <el-alert
          :title="`数据一共 ` + this.pageAll.counts + `条`"
          type="info"
          show-icon
          :closable="false"
        >
        </el-alert>
        <el-table :data="tableData" v-loading="loading" style="width: 100%">
          <el-table-column
            label="序号"
            width="48px"
            type="index"
          ></el-table-column>
          <el-table-column
            label="学科名称"
            width="104px"
            prop="subjectName"
          ></el-table-column>
          <el-table-column
            label="创建者"
            width="99px"
            prop="username"
          ></el-table-column>
          <el-table-column label="创建日期" width="160px" prop="addDate">
            <template v-slot="{ row }">{{ row.addDate }}</template>
          </el-table-column>
          <el-table-column label="前台是否显示" prop="isFrontDisplay">
            <template v-slot="{ row }">
              {{ row.isFrontDisplay === 1 ? '是' : '否' }}
            </template>
          </el-table-column>
          <el-table-column
            label="二级目录"
            prop="twoLevelDirectory"
          ></el-table-column>
          <el-table-column label="标签" prop="tags"></el-table-column>
          <el-table-column label="题目数量" prop="totals"></el-table-column>
          <el-table-column label="操作" width="240px">
            <template v-slot="{ row }">
              <el-button
                type="text"
                @click="$router.push(`/subjects/directorys`)"
                >学科分类</el-button
              ><el-button type="text" @click="$router.push(`/subjects/tags`)"
                >学科标签</el-button
              >
              <el-button type="text" @click="revise(row.id)">修改</el-button>
              <el-button type="text" @click="delList(row)">删除</el-button>
              <el-divider direction="vertical" />
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row type="flex" justify="end">
          <el-pagination
            background
            layout="prev, pager, next,sizes, jumper"
            :total="pageAll.counts"
            :current-page="parseInt(pageAll.page)"
            :page-size="parseInt(pageAll.pageSize)"
            :page-sizes="[10, 20, 30]"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          >
          </el-pagination>
        </el-row>
        <!-- 新增弹框 -->
        <el-dialog
          :title="`${fromData.id ? '修改学科' : '新增学科'}`"
          :visible="showDialog"
          @close="closeDialog"
          width="30%"
        >
          <el-form ref="formRef" :model="fromData" :rules="rules">
            <el-row>
              <el-col>
                <el-form-item label="学科名称" prop="subjectName">
                  <el-input
                    placeholder="请输入学科名称"
                    v-model="fromData.subjectName"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col>
                <el-form-item label="是否显示" prop="isFrontDisplay">
                  <el-switch
                    active-color="#13ce66"
                    inactive-color="#ff4949"
                    v-model="fromData.isFrontDisplay"
                    :active-value="1"
                    :inactive-value="0"
                  ></el-switch>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <el-row justify="end" class="dialog-footer" type="flex">
            <el-button @click="closeDialog">取 消</el-button>
            <el-button type="primary" @click="btnOk">确 定</el-button>
          </el-row>
        </el-dialog>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { list, remove, add, update, detail } from '@/api/hmmm/subjects.js'

export default {
  name: 'Subjects',
  data () {
    return {
      subFrom: {
        user: '',
        region: ''
      },
      loading: false,
      tableData: [],
      showDialog: false,
      pageAll: {
        page: 1,
        pageSize: ''
      },
      fromData: {
        subjectName: '',
        isFrontDisplay: true
      },
      rules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    async getList () {
      const { data } = await list({
        pagesize: this.pageAll.pageSize,
        page: this.pageAll.page
      })
      // console.log(data)
      this.tableData = data.items
      this.pageAll = data
      this.pageAll.page = data.page
    },
    handleSizeChange (newPagesize) {
      this.pageAll.pageSize = newPagesize
      this.getList()
    },
    handleCurrentChange (newPage) {
      this.pageAll.page = newPage
      this.getList()
    },
    // 删除
    async delList (row) {
      await this.$confirm(
        '此操作将永久删除该文件, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        },
        await remove(row)
      )
        .then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    closeDialog () {
      this.showDialog = false
      this.fromData.id = null
      this.fromData.subjectName = ''
    },
    // 清除
    clearIpt () {
      this.subFrom.user = ''
      this.getList()
    },
    // 新增
    async btnOk () {
      await this.$refs.formRef.validate()
      if (!this.fromData.id) {
        await add(this.fromData)
        this.$message.success('添加成功！')
      } else {
        await update(this.fromData)
        this.$message.success('修改成功！')
        this.fromData.id = null
      }
      this.getList()
      this.closeDialog()
    },
    // 修改
    async revise (id) {
      this.showDialog = true
      const res = await detail({ id })
      this.fromData = res.data
    },
    // 搜索
    onSubmit () {
      this.tableData = this.tableData.filter(
        (v) => v.subjectName === this.subFrom.user
      )
    }
  }
}
</script>

<style scoped lang="less">
.container {
  .addbtn {
    margin-left: 290px;
  }
}
</style>
